<?php

namespace Wechat\Controller;

class CaseController extends CommonController {

    public function index() {
        $categoryId = isNullOrEmpty($_REQUEST["categoryId"]) ? "0" : $_REQUEST["categoryId"];

        $map["h.position"] = 6;
        $caseIndex = D("Home")->alias("h")
//            ->join("left join __SCIENCE__ c on c.science_id = h.link")
            ->where($map)
//            ->field("h.*, c.title_image")
            ->select();

        $map["h.position"] = 4;
        $caseList1 = D("Home")->alias("h")
            ->join("left join __CASE__ c on c.case_id = h.link")
            ->where($map)
            ->field("h.*, c.title_image, c.case_id")
            ->select();
        $map["h.position"] = 5;
        $caseList2 = D("Home")->alias("h")
            ->join("left join __CASE__ c on c.case_id = h.link")
            ->where($map)
            ->field("h.*, c.title_image, c.case_id")
            ->select();
//        $map["h.position"] = 7;
//        $caseList3 = D("Home")->alias("h")
//            ->join("left join __CASE__ c on c.case_id = h.link")
//            ->where($map)
//            ->field("h.*, c.title_image, c.case_id")
//            ->select();
        $caseList = array_merge($caseList1, $caseList2);
        $this->assign("caseIndex", $caseIndex);
        $this->assign("caseList", $caseList);
        $this->assign("categoryId", $categoryId);
        $this->display("index");
    }

    public function caseList() {

        $categoryId = I("post.categoryId");

//        if(!isNullOrEmpty($categoryId)) {
//            $map["p.category_id"] = $categoryId;
//        }

//        $list = D("Product")->alias("p")
//            ->join("left join __CATEGORY__ c on c.category_id = p.category_id")
//            ->where($map)
//            ->field("p.product_id,p.product_name,c.is_finished")
//            ->select();
//
//        foreach ($list as &$item) {
//            $itemMap["pd.product_id"] = $item["product_id"];
//            $itemMap["pd.title"] = "病例展示";
//            $item["desc_list"] = D("ProductDesc")->alias("pd")
//                ->where($itemMap)
//                ->select();
//
//        }

            $map["cs.disease"] = $categoryId;

        $list = D("Case")->alias("cs")
//            ->join("left join __PRODUCT__ p on p.product_id = pd.product_id")
            ->where($map)
//            ->field("pd.*")
            ->select();
        $diseaseName = "disease_".$categoryId;
        $customerInfo = $this->customer;
        if(isNullOrEmpty($customerInfo[$diseaseName])) {
            $customerInfo[$diseaseName] = 0;
        }
        $customerInfo[$diseaseName] = $customerInfo[$diseaseName] + 1;
        D("Customer")->updateInfo($customerInfo);
        echo json_encode($list);

    }

    public function detail() {

        $id = $_REQUEST["id"];

        $case = D("Case")->queryById($id);

        if(isNullOrEmpty($case)) {
            $this->redirect(U("case/index"));
        }
        $case["read_count"] = $case["read_count"] + 1;
        D("Case")->updateInfo($case);
        $diseaseName = "disease_".$case["disease"];
        $customerInfo = $this->customer;
        if(isNullOrEmpty($customerInfo[$diseaseName])) {
            $customerInfo[$diseaseName] = 0;
        }
        $customerInfo[$diseaseName] = $customerInfo[$diseaseName] + 1;
        D("Customer")->updateInfo($customerInfo);

//        $category = D("Category")->queryById($product["category_id"]);

        $photoList = explode(",", $case["photo_list"]);

//        $this->assign("category", $category);
        $this->assign("case", $case);
        $this->assign("photoList", $photoList);
        $this->assign("customer", $this->customer);
        $this->display("detail");
    }
}